import Vue from 'vue'
import Router from 'vue-router'

//fix
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch(err => err)
};

Vue.use(Router);

import Layout from '@/layout'
import Layout1 from '@/layout/index1';
import Layout2 from '@/layout/index2';
import Solo from '@/layout/Solo'
// import {
// 	TRUE
// } from 'sass';

//固定路由，不需要根据权限生成
export const constantRoutes = [{
  path: '/redirect',
  component: Layout,
  hidden: true,
  children: [{
    path: '/redirect/:path(.*)',
    component: () => import('@/views/redirect/index')
  }]
},
{
  path: '/login',
  component: () => import('@/views/login/index'),
  hidden: true
},
{
  path: '/auth-redirect',
  component: () => import('@/views/login/auth-redirect'),
  hidden: true
},
{
  path: '/404',
  component: () => import('@/views/error-page/404'),
  hidden: true
},
{
  path: '/401',
  component: () => import('@/views/error-page/401'),
  hidden: true
},
];

//动态路由，读取api权限接口数据后，根据权限数据动态添加到vue-router中
export const asyncRoutes = [
  {
    path: '/project-warningClassification',
    component: Layout,
    name: 'warningClassification',
    meta: {
      title: '预警分类管理',
      icon: 'elevator'
    }, hidden: false,
    redirect: '/project-warningClassification/index',
    children: [{
      path: 'index',
      hidden: true,
      component: () => import('@/views/project-warningClassification/index'),
      name: 'warningClassification',
      meta: {
        title: '预警分类管理',
        // activeMenu: '/statistics/index'
      }
    }]
  },
  {
    path: '/project-warningAbnormality',
    component: Layout,
    name: 'warningAbnormality',
    meta: {
      title: '预警异常管理',
      icon: 'dttd'
    }, hidden: false,
    redirect: '/project-warningAbnormality/index',
    children: [{
      path: 'index',
      hidden: true,
      component: () => import('@/views/project-warningAbnormality/index'),
      name: 'warningAbnormality',
      meta: {
        title: '预警异常管理',
        // activeMenu: '/statistics/index'
      }
    }]
  },
  {
    path: '/project-departments',
    component: Layout,
    name: 'departments',
    meta: {
      title: '部门管理',
      icon: 'tree'
    }, hidden: false,
    redirect: '/project-departments/index',
    children: [{
      path: 'index',
      hidden: true,
      component: () => import('@/views/project-departments/index'),
      name: 'departments',
      meta: {
        title: '部门管理',
        // activeMenu: '/statistics/index'
      }
    }]
  },
  {
    path: '/project-warningAddressBook',
    component: Layout,
    name: 'warningAddressBook',
    meta: {
      title: '预警通讯录',
      icon: 'warningAddressBook'
    }, hidden: false,
    redirect: '/project-warningAddressBook/index',
    children: [{
      path: 'index',
      hidden: true,
      component: () => import('@/views/project-warningAddressBook/index'),
      name: 'warningAddressBook',
      meta: {
        title: '预警通讯录',
        // activeMenu: '/statistics/index'
      }
    }]
  },
  {
    path: '/project-cameraManage',
    component: Layout,
    name: 'cameraManage',
    meta: {
      title: '摄像头管理',
      icon: 'cameraManage'
    }, hidden: false,
    redirect: '/project-cameraManage/index',
    children: [{
      path: 'index',
      hidden: true,
      component: () => import('@/views/project-cameraManage/index'),
      name: 'cameraManage',
      meta: {
        title: '摄像头管理',
        // activeMenu: '/statistics/index'
      }
    }]
  },
  {
    path: '/project-alarmHandling',
    component: Layout,
    name: 'alarmHandling',
    meta: {
      title: '告警处理',
      icon: 'alarmHandling'
    }, hidden: false,
    redirect: '/project-alarmHandling/index',
    children: [{
      path: 'index',
      hidden: true,
      component: () => import('@/views/project-alarmHandling/index'),
      name: 'alarmHandling',
      meta: {
        title: '告警处理',
        // activeMenu: '/statistics/index'
      }
    }]
  },

  {
    path: '/system',
    component: Layout,
    name: 'System',
    meta: {
      title: '系统管理',
      icon: 'menu-xtgl'
    },
    redirect: '/system/user',
    children: [{
      path: 'user',
      component: () => import('@/views/system/user/index'),
      name: 'SystemUser',
      meta: {
        title: '用户管理'
      }
    },
    {
      path: 'menu',
      component: () => import('@/views/system/menu'),
      name: 'Menu',
      meta: {
        title: '菜单管理'
      }
    },
    {
      path: 'role',
      component: () => import('@/views/system/role'),
      name: 'Role',
      meta: {
        title: '角色管理'
      }
    },
    {
      path: 'authority',
      name: 'Authority',
      component: () => import('@/views/system/authority/index'),
      redirect: '/system/authority/tab1',
      meta: {
        title: '权限管理'
      }, //tabIndex
      children: [{
        path: 'tab1',
        //component: () => import('@/views/system-manage/authority/index'),
        name: 'Authority1',
        meta: {
          title: '角色权限管理'
        }
      },
      {
        path: 'tab2',
        //component: () => import('@/views/system-manage/authority/index'),
        name: 'Authority2',
        meta: {
          title: '人员角色管理'
        }
      },
      {
        path: 'tab3',
        //component: () => import('@/views/system-manage/authority/index'),
        name: 'Authority3',
        meta: {
          title: '角色人员管理'
        }
      },
      {
        path: 'tab4',
        //component: () => import('@/views/system-manage/authority/index'),
        name: 'Authority4',
        meta: {
          title: '权限角色管理'
        }
      },
      ]
    },
    {
      path: 'organization',
      component: () => import('@/views/system/organization'),
      name: 'Organization',
      meta: {
        title: '组织机构'
      }
    },
    {
      path: 'log',
      component: () => import('@/views/system/log/index'),
      name: 'SystemLog',
      meta: {
        title: '日志管理'
      }
    },
    {
      path: 'params',
      component: () => import('@/views/system/params'),
      name: 'SystemParams',
      meta: {
        title: '系统配置'
      }
    },
    {
      path: 'dictionary',
      component: () => import('@/views/system/dictionary/index'),
      name: 'SystemDictionary',
      meta: {
        title: '数据字典'
      }
    },


    ]
  },
];

//白名单路由
export const wildcardRoutes = [
  // 404 page must be placed at the end !!!
  {
    path: '*',
    redirect: '/404',
    hidden: true
  },
];

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
});

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher // reset router
}

export default router
